package com.zsc.externalservice.controller;

import com.zsc.externalservice.entity.Product;
import com.zsc.externalservice.entity.vo.CigaretteVO;
import com.zsc.externalservice.services.CigaretteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;


@RestController
@RequestMapping("/api/cigarette")
//@Api(tags = "获取香烟消息相关接口")
@Slf4j
public class CigaretteController {

    @Resource
    private CigaretteService cigaretteService;

    /**
     * 根据名称查询卷烟数据
     * @param name 卷烟名称
     * @return Product
     */
    @GetMapping("getCigaretteByName")
//    @ApiOperation("根据名称获取数据")
    public Product getCigaretteByName(@RequestHeader(name = "name") String name){
        log.info("本次要查询的数据是(未解码)：{}", name);
        // 解码
        String decodedString;
        try {
            decodedString = URLDecoder.decode(name, String.valueOf(StandardCharsets.UTF_8));
        } catch (UnsupportedEncodingException e) {
            log.error("解码失败，本次要解码的内容是：{}", name);
            return null;
        }
        log.info("解码后的数据是，name:{}", decodedString);
        return cigaretteService.getCigaretteByName(decodedString);
    }

    /**
     * 根据卷烟代码code查询信息
     * @param productCode 卷烟代码
     * @return CigaretteVO
     */
    @GetMapping("/getByCode")
//    @ApiOperation("根据卷烟代码code查询信息")
    public CigaretteVO getByCode(String productCode) {
        return cigaretteService.getByCode(productCode);
    }
}
